package com.qf.param;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.file.controller.FileController;
import com.qf.pickname.entity.Student;
import com.qf.pickname.vo.StudentVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;

/**
 * 演示 springmvc接收参数
 */
@RestController
@RequestMapping("/p")
public class ParamController {

    @Value("${file.pic}")
    private String fileDir;

    @RequestMapping("/up")
    // 如何接收文件上传的请求
    public HashMap<String,Object> upload(MultipartFile file01,MultipartFile file02) throws IOException {
        System.out.println(file01);
        System.out.println(file02);
        //1. 文件在服务器上存放的目录
//        String fileDir = FileController.FILE_DIR;

        //2. 给文件起个文件名
//        String originalFilename = file01.getOriginalFilename();
        String filename = UUID.randomUUID().toString();
        String filepath = fileDir + filename;

        //3. 存放文件到指定目录下
        file01.transferTo(new File(filepath));

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code",1);
        hashMap.put("msg","ok");
        return hashMap;
    }


    // 接收请体中的json的方案3 ，使用 @RequestBody HashMap<String,Object> map
    @RequestMapping("/json2")
    public HashMap<String,Object> receiveJson2(@RequestBody String json) throws Exception {
        System.out.println(json);
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap hashMap = objectMapper.readValue(json, HashMap.class);
        return hashMap;
    }


    // 接收请体中的json的方案2 ，使用 @RequestBody HashMap<String,Object> map
    @RequestMapping("/json1")
    public HashMap<String,Object> receiveJson1(@RequestBody HashMap<String,Object> map){
        System.out.println(map);
        return map;
    }

    // 如果请求体中的数据是json，应该如何接收 ,添加注解@RequestBody
    @RequestMapping("/json")
    public StudentVO receiveJson(@RequestBody StudentVO studentVO){
        System.out.println(studentVO);
        return studentVO;
    }

    //http://localhost:9999/p/get/19/luffy
    @RequestMapping("/get/{id}/{name}")
    public StudentVO getStudentById(@PathVariable("id") Integer id,@PathVariable("name") String name){
        System.out.println(id);
        System.out.println(name);
        StudentVO studentVO = new StudentVO();
        studentVO.setName(name);
        studentVO.setAge(id);
        return studentVO;
    }


    //http://localhost:9999/p/arr1?hobby=football&hobby=basketball
    @RequestMapping("/arr1")
    public StudentVO registWithArr1(StudentVO studentVO){
        System.out.println(studentVO);
        return studentVO;
    }


    //http://localhost:9999/p/arr?hobby=football&hobby=basketball
    @RequestMapping("/arr")
    public StudentVO registWithArr(String[] hobby){
        for (String s : hobby) {
            System.out.println(s);
        }

        StudentVO studentVO = new StudentVO();
        studentVO.setHobby(hobby);
        return studentVO;
    }


    // http://localhost:9999/p/registplus?age=20&name=luffy&sex=true&birth=2000-01-01%2010:10:10
    @RequestMapping("/registplus")
    public StudentVO registWithEntity(StudentVO studentVO){
        System.out.println(studentVO);
        return studentVO;
    }


    // http://localhost:9999/p/regist?age=20&name=luffy&sex=true&birth=2000-01-01%2010:10:10
    @RequestMapping("/regist")
    public StudentVO regist(Integer age, String name, Boolean sex,
                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date birth){

        System.out.println(age);
        System.out.println(name);
        System.out.println(sex);
        System.out.println(birth);

        StudentVO student = new StudentVO();
        student.setName(name);
        student.setAge(age);
        student.setBirth(birth);
        student.setSex(sex);

        return student;
    }

}
